package com.chaochis.wind.base_controller;

import com.chaochis.wind.base_config.page_config.api.ApiCode;
import com.chaochis.wind.base_config.page_config.api.ApiResult;
import com.chaochis.wind.base_exceptions.serve_exception.DataNotActiveException;
import com.chaochis.wind.base_exceptions.serve_exception.DataNotExistException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理类
 * @author 刘超/chaochis
 */
@RestControllerAdvice
public class CommonExceptionController {

  @ExceptionHandler({DataNotExistException.class})
  public ApiResult<Boolean> dataNotExistExceptionHandler(DataNotExistException e) {
    return ApiResult.build(e.getMessage(), null, ApiCode.DataNotExist, null);
  }

  @ExceptionHandler({DataNotActiveException.class})
  public ApiResult<Boolean> dataNotActiveExceptionHandler(DataNotActiveException e) {
    return ApiResult.build(e.getMessage(), null, ApiCode.DataNotActive, null);
  }

  /**
   * 通用异常处理类
   * @param e 异常信息
   * @return 返回的结果
   */
  @ExceptionHandler(Exception.class)
  public ApiResult<Boolean>exceptionHandler(Exception e) {
      return ApiResult.fail(e);
  }
}
